class SaleController < ApplicationController
    before_filter :login_required
    layout "framework",:except=>[:select_product,:show_print_sale_paper,:select_buyer]

    def checkSalePaper(paperId,state)
        lackProduct = ''
        #debugger 
        if state.eql? 'OK'
            #对比库存
            #取默认出货仓库
            defStore = Store.find(:first,:conditions=>"out_type=1")
            #对比库存
            addressSalePapers = Array.new
            salePaper = SalePaper.find(paperId)
            storeProduct = nil
            #Product = nil
            salePaper.salePaperItems.each do |item|
                puts "product_id=#{item.product_id} and store_id=#{defStore.id}"
                storeProduct = StoreProduct.find(:first,:conditions=>"product_id=#{item.product_id} and store_id=#{defStore.id}")
                tmpCount = 0
                tmpCount = storeProduct.current_count if storeProduct
                if item.current_count > tmpCount
                    lackProduct = item.product.name
                    break
                else
                    addressSalePaper = AddressSalePaper.new(storeProduct.id,storeProduct.product_id,item.current_count)
                    addressSalePapers << addressSalePaper
                end
            end
            #debugger 
            #处理帐目
            if lackProduct.eql? ''
                addressSalePapers.each do |asp|
                #增加商品数量变更帐目
                productTransactionItem = ProductTransactionItem.new
                productTransactionItem.create_date = Time.now
                productTransactionItem.type = 'SA'
                productTransactionItem.value = 0 - asp.value
                productTransactionItem.product_id = asp.productId
                productTransactionItem.save
                #改变商品库存值
                tmpStoreProduct = StoreProduct.find(asp.storeProductId)
                tmpStoreProduct.current_count = tmpStoreProduct.current_count - asp.value
                tmpStoreProduct.save
                #改变商品总量
                tmpProduct = Product.find(:all,:conditions=>"id in (select product_id from store_products where product_id=#{asp.productId} )")
                if tmpStoreProduct.save
                    sumTotalcounts = StoreProduct.find_by_sql("select sum(current_count) as a from store_products where product_id=#{asp.productId}") 
                    #update products set total_counts = ( where id=#{@storeProduct.product.id}
                    tmpProduct[0].total_counts = sumTotalcounts[0].a.to_f
                    tmpProduct[0].update
                end
                #增加商品库存变更帐目
                storeTransactionItem = StoreTransactionItem.new
                storeTransactionItem.current_count = 0 - asp.value
                storeTransactionItem.type = 'SA'
                storeTransactionItem.create_date_at = Time.now
                storeTransactionItem.store_product_id = asp.storeProductId
                storeTransactionItem.save
                end
            end
        end
        if !lackProduct.eql? ''
            flash[:notice] = "<img src='/images/fail.gif'><font color='red'>#{lackProduct}商品在出货仓库#{defStore.name}中已经存货不足,请调配商品或补充存货！</font>"
        elsif paperId
            SalePaper.update(paperId,:papertype=>state)
            flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{paperId}号订单已经审批！</font>"
        end
    end

    def index
        @session[:rights] = buildMenu '销售管理'
    end

    def add_sale_paper
        if !@session[:account].role.memo.eql? 'salemanager'
            @agents = Agent.find(:all,:conditions=>"employee_id=#{@session[:account].employee_id}")
        else
            @agents = Agent.find_all
        end
        @myname = ConfigVariable.find_by_name('myname')
        @myaddress = ConfigVariable.find_by_name('myaddress')
        @myphone = ConfigVariable.find_by_name('myphone')
        @myfax   = ConfigVariable.find_by_name('myfax')			

        if !request.get?
            @salePaper = SalePaper.new(params[:salePaper]) 
            @salePaper.papertype = 'UN'
            @salePaper.sale_date_at = Time.now
            if @salePaper.amount_of_insurance or @salePaper.amount_of_insurance == nil
                @salePaper.amount_of_insurance = 0;
            end
            if @salePaper.box_count or @salePaper.box_count == nil
                @salePaper.box_count = 0;
            end
            @salePaper.employee_id = @session[:account].employee_id
            @salePaper.save
            #add sale_paper_item
            totalPrice = 0.0

            #取默认出货仓库
            #defStore = Store.find(:first,:conditions=>"out_type=1")

            hidProductInfo = params[:hidProductInfo].to_s
            hidProductInfo.each(';') do |product|
                i = 0
                paperItem = SalePaperItem.new
                paperItem.state = 'UN'
                paperItem.get_date_at =  Time.now
                paperItem.sale_paper_id = @salePaper.id
                product.delete(';').each(',') do |item|
                    if i == 0
                        paperItem.product_id = item.delete(',').to_i
                    elsif i == 1
                        paperItem.current_count = item.delete(',').to_f
                    elsif i == 2
                        paperItem.price = item.delete(',').to_f
                    elsif i == 3
                        paperItem.memo = item.delete(',')
                    elsif i == 4
                        paperItem.goods_num = item.delete(',')
                    end
                    i = i+1;
                end
                totalPrice += paperItem.current_count * paperItem.price
                paperItem.save
            end
            #======少于300的订单直接算是审批通过================================================
            if totalPrice < 300
                #debugger
                checkSalePaper @salePaper.id,'OK'
            end
            #++++记录message
            roles = Role.find(:all,:conditions=>"memo like '%salemanager%'")
            roles.each do |role|
                role.accounts.each do |account|
                    message=Message.new
                    message.employee_id=account.employee.id
                    message.message='该订单已经自动确认'
                    message.relativelyId=@salePaper.id
                    message.messageType='SA'
                    message.checked=0
                    message.create_date_at=Time.now
                    message.save
                end
            end
            #============================
            flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{@salePaper.agent.name}的订单已经成功增加</font>"
        end
    end

    def select_product
        if request.get? and params[:optype]==nil
            state = 1
            if params[:state]=='0'
                state = 0
            end 
            @product_pages, @products = paginate(:products,:conditions=>"state = #{state}",:order=>'in_date_at DESC',:per_page => 20)
        elsif !request.get? and params[:optype].eql? 'search'
            #@products = Product.find(:all,:conditions=>'state=1',:order=>'in_date_at DESC')
            product_serial = params[:product_serial]
            product_name = params[:product_name]
            conditions = ''
            #if product_name != nil && product_name != ''
            #	conditions += "name like '%#{product_name}%'"
            #end
            #if product_serial != nil && product_serial != ''
            #	if conditions != ''
            #    	conditions += ' and '
            #			end
            #    	conditions += "serial like '%#{product_serial}%'"
            #    end
            conditions += " serial like '%#{product_serial}%' and name like '%#{product_name}%' and state=1"
            puts conditions
            @product_pages, @products = paginate(:products,:conditions=>conditions,:per_page => 20)
        elsif !request.get? and params[:optype].eql? 'select'
            #获取数据并显示在父页
        end
    end

    def select_buyer
    end

    def list_sale_paper_4_RE
        @type = 'RE'
        ctype = "(papertype='NDFD' )"
        if !@session[:account].role.memo.eql? 'salemanager'
            ctype += " and employee_id=#{@session[:account].employee.id}"
        end
        @sale_paper_pages, @sale_papers = paginate(:sale_papers,:conditions=>"#{ctype}",:order=>'sale_date_at DESC',:per_page => 20)
        render :template=>'sale/list_sale_paper'
    end

    def list_sale_paper
        @type = params[:type]
        paper_id = params[:paper_serial]
        from = params[:from]
        to = params[:to]
        #debugger
        if request.get?
            ctype = nil
            if params[:type].eql? 'UN'
                ctype = "papertype='UN'"
            elsif params[:type].eql? 'ED'
                ctype = "(papertype='OK' or papertype='CA' or papertype='NO' or papertype='NDFD' or papertype='RE')"
            elsif params[:type].eql? 'RE'
                ctype = "(papertype='NDFD' or papertype='OK' or papertype='GO' or papertype='ED' or papertype='RE')"
            end 
            if ctype
                if !@session[:account].role.memo.eql? 'salemanager'
                    ctype += " and employee_id=#{@session[:account].employee.id}"
                end
                @sale_paper_pages, @sale_papers = paginate(:sale_papers,:conditions=>"#{ctype}",:order=>'sale_date_at DESC',:per_page => 20)
            else
                if @session[:account].role.memo.eql? 'salemanager'
                    @sale_paper_pages, @sale_papers = paginate(:sale_papers,:order=>'sale_date_at DESC',:per_page => 20)
                else
                    @sale_paper_pages, @sale_papers = paginate(:sale_papers,:conditions=>"employee_id=#{@session[:account].employee.id}",:order=>'sale_date_at DESC',:per_page => 20)
                end
            end
        else
            conditions = ''
            if paper_id and !paper_id.eql? ''
                conditions += "id=#{paper_id}"
            elsif from and to and !from.eql? '' and !to.eql? ''
                conditions += "sale_date_at between '#{from}' and '#{to}'"
            end

            if params[:papertype].eql? 'RE'
                if !conditions.eql? ''
                    conditions += " and "
                end
            conditions += "(papertype='NDFD' or papertype='OK' or papertype='GO' or papertype='ED')"
            end
            #debugger
            if !conditions.eql? ''
                if !@session[:account].role.memo.eql? 'salemanager'
                    conditions += " and employee_id=#{@session[:account].employee.id}"
                end
            @sale_paper_pages, @sale_papers = paginate(:sale_papers,:conditions=>conditions,:per_page => 20)
            else
                if !@session[:account].role.memo.eql? 'salemanager'
                    @sale_papers = SalePaper.find(:all,:conditions=>"employee_id=#{@session[:account].employee.id}")
                else
                    @sale_paper_pages, @sale_papers = paginate(:sale_papers,:order=>'sale_date_at DESC',:per_page => 20)
                end
            end
        end
    end

    def show_sale_paper
        @sale_paper = SalePaper.find(params[:id])
    end

    def show_print_sale_paper
        show_sale_paper
    end

    def edit_sale_paper
        if !@session[:account].role.memo.eql? 'salemanager'
            @agents = Agent.find(:all,:conditions=>"employee_id=#{@session[:account].employee_id}")
        else
            @agents = Agent.find_all
        end
        if request.get?
            @salePaper = SalePaper.find(params[:id])
        else
            paper = SalePaper.new params[:salePaper]
            if paper.save
                flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{paper.id}号订单数据信息已修改！</font>"
            end
        end
    end

    def address_sale_paper_4_AD
        @pageType = 'AD'
        condition = "papertype='UN'"
        @sale_papers = SalePaper.find(:all,:conditions=>condition,:order=>'sale_date_at DESC')
        render :template=>'sale/address_sale_paper'
    end

    def address_sale_paper_4_NU
        @pageType = 'NU'
        condition = "papertype='OK' or papertype='GO'"
        @sale_papers = SalePaper.find_by_sql("select * from sale_papers where papertype='OK' or papertype='GO' and id in (select distinct sale_paper_id from sale_paper_items where state='UN') order by sale_date_at DESC")
        render :template=>'sale/address_sale_paper'
    end

    def address_sale_paper_4_GO
        @pageType = 'GO'
        condition = "papertype='OK'"
        @sale_papers = SalePaper.find(:all,:conditions=>condition,:order=>'sale_date_at DESC')
        render :template=>'sale/address_sale_paper'
    end

    def address_sale_paper
        #==============================确定显示内容
        @pageType = params[:pageType]
        @pageType = 'AD' if !@pageType
        condition = "papertype='UN'"
        if @pageType.eql? 'AD'   #显示审批
            condition = "papertype='UN'"
        elsif @pageType.eql? 'NU'   #显示回填条形码
            condition = "papertype='OK' or papertype='GO'"
        elsif @pageType.eql? 'GO'   #显示补齐订单
            condition = "papertype='OK' or papertype='NU'"
        end
        #==============================处理审批=========================================
        #debugger	
        checkSalePaper params[:id],params[:state]
        if @pageType.eql? 'NU'
            @sale_papers = SalePaper.find_by_sql("select * from sale_papers where papertype='OK' or papertype='GO' and id in (select distinct sale_paper_id from sale_paper_items where state='UN') order by sale_date_at DESC")
        else
            @sale_papers = SalePaper.find(:all,:conditions=>condition,:order=>'sale_date_at DESC')
        end

        #===========================处理查询==============================
        if !request.get?
            @pageType=params[:pageType] 
            paper_id = params[:paper_serial]
            from = params[:from]
            to = params[:to]
            conditions = "select * from sale_papers where papertype='OK' or papertype='GO' and id in (select distinct sale_paper_id from sale_paper_items where state='UN')"
            if paper_id and !paper_id.eql? ''
                conditions += " and id=#{paper_id}"
            elsif from and to and !from.eql? '' and !to.eql? ''
                conditions += " and sale_date_at between '#{from}' and '#{to}'"
            end

            @sale_papers = SalePaper.find_by_sql(conditions)
        end
    end

    def full_sale_paper
        if request.get?
            @salePaper = SalePaper.find(params[:paperid])
            @salePaperitem = SalePaperItem.find(params[:id])
        else
            tmpsalePaper = SalePaper.find(params[:paper_id])
            tmpsalePaperitem = SalePaperItem.find(params[:item_id])
            tmpsalePaperitem.serial = params[:serial]
            tmpsalePaperitem.get_date_at = params[:get_date_at]
            tmpsalePaper.box_count = params[:boxcount].to_f
            tmpsalePaperitem.save
            
            @session[:item_id] = params[:item_id]
            p1 = SalePaperItem.find_by_sql("select serial from sale_paper_items where  serial =0 and sale_paper_id = #{params[:paper_id]}")
            if p1 != [] 
                flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{params[:item_id]}产品补齐成功！</font>"
                    redirect_to :action => "address_sale_paper", :pageType => 'GO'

            else
                if tmpsalePaper.papertype == 'OK' and tmpsalePaper.charge.eql? 'yes' #and tmpsalePaperitem.serial ! ==0
                    tmpsalePaper.papertype = 'NDFD'
                    tmpsalePaper.save
                    flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{params[:paper_id]}号订单已经完成！</font>"
                    redirect_to :action => "list_sale_paper"
                elsif tmpsalePaper.papertype == 'OK' 
                    tmpsalePaper.papertype = 'GO'
                    tmpsalePaper.save
                    flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{params[:paper_id]}号订单已经补齐出库单号！</font>"
                    redirect_to :action => "address_sale_paper", :pageType => 'GO'
                end
            end
        end

    end

    def list_sale_paper_item
        @type = params[:type]
        conditions = "sale_paper_id=#{params[:salePaperId]} and status=0"
        #if @type.eql? 'NU'
        #conditions = "sale_paper_id=#{params[:salePaperId]} and state='UN'"
        #end
        if params[:salePaperId]
            @salePaperItems = SalePaperItem.find(:all,:conditions=>conditions)
            @salePaperId = params[:salePaperId]
            #@agent = params[:agent]
            @agentId = params[:agentId]
        end
    end

    def add_sale_number
        @agent = params[:agent]
        @agentId = params[:agentId]
        if params[:itemId]
            @item = SalePaperItem.find(params[:itemId])
            if @item.codeCount == nil
                @item.codeCount = 0
            end
        else
            fromNum = params[:fromNum]
            prefix  = params[:prefix]
            postfix = params[:postfix]
            prefix = '' if prefix==nil
            postfix = '' if postfix==nil
            salePaperItem = SalePaperItem.find(params[:salePaperItemId])
            productCount = params[:codeCount].to_i
            #监查条形码是否重复
            strFrom = formatSaleNum(fromNum.to_i, fromNum.length)
            strTo   = formatSaleNum(fromNum.to_i+productCount, fromNum.length)
            c = Code.count_by_sql("select count(*) from codes where prefix='#{prefix}' and postfix='postfix' and code between #{strFrom} and #{strTo}")
            if c != 0
                flash[:notice] = "<img src='/images/fail.gif'><font color='green'>条形码重复,请更换条形码</font>"
                return
            end
            #生成新条形码
            @codes = Array.new
            out_date = salePaperItem.salePaper.sale_date_at
            for i in 0...productCount
                num = formatSaleNum(fromNum.to_i+i, fromNum.length)
                code = Code.new
                code.product_id = salePaperItem.product_id
                code.agent_id = @agentId
                code.code = num
                code.prefix = prefix
                code.postfix = postfix
                code.out_date_at = out_date
                @codes<< code
                code.save
            end
            if @codes.length or @codes.length == nil
                codes_length = 0
            end
            if salePaperItem.codeCount or salePaperItem.codeCount == nil
                salePaperItem.codeCount = 0;
            end
            salePaperItem.state = 'ED'
            salePaperItem.codeCount = salePaperItem.codeCount + codes_length
            salePaperItem.save
            #处理订单状态  #填写条形码不需要修改订单状态#
            #pCount = SalePaperItem.count("state='UN' and sale_paper_id=#{salePaperItem.sale_paper_id}")
            #if pCount == 0
            #	tmpSalePaper = salePaperItem.salePaper
            #	if tmpSalePaper.papertype.eql? 'GO'
            #		tmpSalePaper.papertype = 'NDFD'
            #	end
            #	tmpSalePaper.save
            #end
            render :template=>'sale/list_codes'
        end
    end

    def add_return_paper
        @paper_item = SalePaperItem.find(params[:sale_paper_item_id])
        tmpCount = ReturnPaperItem.find_by_sql("select sum(current_count) as ccount from return_paper_items where sale_paper_item_id=#{params[:sale_paper_item_id]}")
        @return_count = 0
        @return_count = tmpCount[0].ccount if tmpCount[0].ccount
        if !request.get?
            #生成退货单对象
            returnPaperItem = ReturnPaperItem.new
            returnPaperItem.product_id = params[:product_id]
            returnPaperItem.sale_paper_item_id = params[:sale_paper_item_id]
            returnPaperItem.sale_paper_id = @paper_item.sale_paper_id
            returnPaperItem.serial = @paper_item.serial
            returnPaperItem.create_date_at = Time.now
            returnPaperItem.current_count = params[:returnCount]
            returnPaperItem.price = params[:price].to_f * params[:returnCount].to_f
            returnPaperItem.memo = params[:memo]
            returnPaperItem.save
            #记录单个退货产品信息
            if params[:number] && params[:number].length != 0
                params[:number].each(',') do |num|
                    pp = ProblemProduct.new
                    pp.product_id = params[:product_id]
                    pp.code = num.delete ','
                    pp.in_date_at = Time.now
                    pp.save
                end
            end

            SalePaperItem.update(returnPaperItem.sale_paper_item_id, :status=>1);

            salePaperItems = SalePaperItem.find_by_sql("select count(*) as count from sale_paper_items where sale_paper_id=#{returnPaperItem.sale_paper_id} and status=0")
            if salePaperItems[0].count.to_f == 0.00
                SalePaper.update(returnPaperItem.sale_paper_id, :papertype=>'RE')
            end

            #取默认出货仓库
            defStore = Store.find(:first,:conditions=>"out_type=1")
            storeProduct = StoreProduct.find_by_sql("select * from store_products where product_id=#{returnPaperItem.product_id} and store_id=#{defStore.id}")
            #debugger
            if StoreProduct
                storeProduct[0][:current_count] += returnPaperItem.current_count
                storeProduct[0].update
            end
            #改变商品总量
            tmpProduct = Product.find(:all,:conditions=>"id in (select product_id from store_products where product_id=#{returnPaperItem.product_id} )")
            if storeProduct[0].save
                @sumTotalcounts = StoreProduct.find_by_sql("select sum(current_count) as a from store_products where product_id=#{returnPaperItem.product_id}") 
                #update products set total_counts = ( where id=#{@storeProduct.product.id}
                tmpProduct[0].total_counts = @sumTotalcounts[0].a.to_f
                tmpProduct[0].update
            end	
            flash[:notice] = "<img src='/images/ok.gif'><font color='green'>退货单填写成功</font>"
            redirect_to :action => "list_sale_paper", :type => 'RE'
        end
    end

    def charge_sale_paper

        if params[:id]
            tmpsalePaper = SalePaper.find(params[:id])
            if tmpsalePaper.papertype == 'GO'

                SalePaper.update(params[:id],:charge=>'yes',:papertype=>'NDFD')
                flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{params[:id]}号订单已经完成</font>"
            elsif
                #debugger
                if params[:method]=='charge'
                    SalePaper.update(params[:id],:charge=>'yes')
                    flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{params[:id]}号订单已经设置为已支付</font>"
                else
                    SalePaper.update(params[:id],:charge=>'no')
                    flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{params[:id]}号订单已经设置为未支付</font>"
                end
            end

            #SalePaper.update(params[:id],:charge=>'yes')
            #flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{params[:id]}号订单已经设置为已支付</font>"
        end
        if !request.get?
            paper_id = params[:paper_serial]
            from = params[:from]
            to = params[:to]
            conditions = ''

            if paper_id and !paper_id.eql? ''
                conditions += "id=#{paper_id}"
            elsif from and to and !from.eql? '' and !to.eql? ''
                conditions += "sale_date_at between '#{from}' and '#{to}'"
            end
            if !conditions.eql? ''
                if !@session[:account].role.memo.include? 'salemanager'
                    conditions += " and employee_id=#{@session[:account].employee.id}"
                end
            if 'yes'.eql? params[:charged]
                conditions += " and charge='yes'"
            else
                conditions += " and charge='no'"
            end
            conditions += " and papertype<>'CA' and papertype<>'NO' and papertype<>'UN' and papertype<>'NDFD' and  papertype<>'RE'"
            @sale_papers = SalePaper.find(:all,:conditions=>conditions,:order=>'sale_date_at DESC')
            else
                if 'yes'.eql? params[:charged]
                    conditions += "charge='yes'"
                else
                    conditions += "charge='no'"
                end
                conditions += " and papertype<>'CA' and papertype<>'NO' and papertype<>'UN' and papertype<>'NDFD'  and  papertype<>'RE'"

                if !@session[:account].role.memo.include? 'salemanager'
                    conditions += " and employee_id=#{@session[:account].employee.id}"
                    @sale_papers = SalePaper.find(:all,:conditions=>conditions)
                else
                    @sale_papers = SalePaper.find(:sale_papers,:conditions=>conditions,:order=>'sale_date_at DESC')
                end
            end
        else
            @sale_papers = SalePaper.find(:all,:conditions=>"charge='no' and papertype<>'CA' and papertype<>'NO' and papertype<>'UN' and papertype<>'NDFD'  and  papertype<>'RE'",:order=>'sale_date_at DESC')
        end
    end

    def query_code
        if !request.get?
            prefix = params[:prefix]
            postfix = params[:postfix]
            from = params[:from]
            to = params[:to]
            @codes = Code.find(:all,:conditions=>"prefix='#{prefix}' and postfix='#{postfix}' and code between #{from} and #{to}")
        end
    end

    def delete_code
        i=0
        params[:codes].each(",") do |code|
            i = i+1
            Code.delete(code.delete(','))			
        end
        flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{i}个条形码记录已经删除</font>"
        redirect_to :action => "query_code"
    end

    def stop_sale_paper
            #debugger
            #已补齐和已通过的才需要更新数据库中的数据
            if params[:papertype] == 'OK' || params[:papertype] == 'GO' 
                    #取默认出货仓库
                    paperItem = SalePaperItem.find(:all,:conditions=>"sale_paper_id=#{params[:id]}")
                    #debugger
                    paperItem.each do |item|
                            defStore = Store.find(:first,:conditions=>"out_type=1")
                            storeProduct = StoreProduct.find_by_sql("select * from store_products where product_id=#{item.product_id} and store_id=#{defStore.id}")
                            #debugger
                            if StoreProduct
                                    storeProduct[0][:current_count] += item.current_count.to_f
                                    storeProduct[0].update
                            end
                            #改变商品总量
                            tmpProduct = Product.find(:all,:conditions=>"id in (select product_id from store_products where product_id=#{item.product_id} )")
                            if storeProduct[0].save
                                    @sumTotalcounts = StoreProduct.find_by_sql("select sum(current_count) as a from store_products where product_id=#{item.product_id}") 
                                    #update products set total_counts = ( where id=#{@storeProduct.product.id}
                                    tmpProduct[0].total_counts = @sumTotalcounts[0].a.to_f
                                    tmpProduct[0].update
                            end	
                    end
            end
        SalePaper.update(params[:id], :papertype=> "CA")
        redirect_to(:action=>'list_sale_paper')
    end
end
